package org.bright.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.bright.entity.User;
import org.bright.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("user")
@ResponseBody
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userServiceImpl;
    @GetMapping("getAll")
    private List<User> getUsers(@RequestParam("name")String name, @RequestParam("age") Integer age){
        List<User> users = userServiceImpl.getUsers(name, age);
        return users;
    }
    @RequestMapping("deleteUsers")
    private int deleteUsers(@RequestParam(value = "id")int id){
        return userServiceImpl.deleteById(id);
    }
    @PostMapping(value = "addUsers")
    private int addUsers(@RequestBody String[] user) {
        System.out.println(Arrays.toString(user));
        User user1 = new User();
        user1.setId(Integer.parseInt(user[0]));
        user1.setName(user[1]);
        user1.setAge(Integer.parseInt(user[2]));
        int insert = userServiceImpl.insert(user1);
        return insert;
    }
    @PostMapping(value = "updateUsers")
    private int updateUsers(@RequestBody String[] user){
        System.out.println("\n\n\n\n\n");
        System.out.println(Arrays.toString(user));
        User user1 = new User();
        user1.setId(Integer.parseInt(user[0]));
        user1.setName(user[1]);
        user1.setAge(Integer.parseInt(user[2]));
        System.out.println(user1);
        int updateUser = userServiceImpl.updateUser(user1);
        return updateUser;
    }

    @GetMapping("getPage")
    public PageInfo<User> getPage(@RequestParam("name")String name, @RequestParam(value = "age") Integer age, @RequestParam("pageNum") Integer pageNum){
        PageInfo<User> page = userServiceImpl.getPage(name, age, pageNum);
        return page;
    }
}
